/**
 * Project Name:cnsy-url-short-service
 * File Name:PageAddrCacheListener.java
 * Package Name:cn.cnsy123.shorturls.business.listener 
 * Date: 2020年4月14日-上午10:47:12
 * Copyright (c) 2020, yangpengfei@360@126.com All Rights Reserved. 
 *
 */

package cn.cnsy123.shorturls.business.listener;

import java.io.File;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.cnsy123.shorturls.cache.PageAddrCache;

/**
 * ClassName: PageAddrCacheListener
 * 
 * @author yangpengfei
 * @date 2020年4月14日-上午10:47:12
 * @version 1.0
 * @since JDK 1.7
 */
@WebListener
public class PageAddrCacheListener implements ServletContextListener {
    
    private static final Logger logger = LoggerFactory.getLogger(PageAddrCacheListener.class);

    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        String viewFilePath = servletContextEvent.getServletContext().getRealPath("/WEB-INF/views");
        String[] fileList = new File(viewFilePath).list();
        for(int i = 0; i < fileList.length; i++) {
            logger.info("copy file name :[{}]",FilenameUtils.getBaseName(fileList[i]));
            PageAddrCache.writeData(FilenameUtils.getBaseName(fileList[i]));
        }
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        PageAddrCache.clear();
    }

}
